package com.ruoyi.web.controller.wms;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.wms.service.IWmsMaterialCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsMaterialInfo;
import com.ruoyi.wms.service.IWmsMaterialInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料信息管理Controller
 * 
 * @author 杨懿
 * @date 2023-11-27
 */
@Controller
@RequestMapping("/wms/materialInfo")
public class WmsMaterialInfoController extends BaseController
{
    private String prefix = "wms/materialInfo";

    @Autowired
    private IWmsMaterialCategoryService wmsMaterialCategoryService;

    @Autowired
    private IWmsMaterialInfoService wmsMaterialInfoService;

    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("wms:materialInfo:view")
    @GetMapping()
    public String materialInfo()
    {
        return prefix + "/materialInfo";
    }

    /**
     * 查询物料信息管理列表
     */
    @RequiresPermissions("wms:materialInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsMaterialInfo wmsMaterialInfo)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()){
            if(wmsMaterialInfo.getDeptId() == null){
                wmsMaterialInfo.setDeptId(ShiroUtils.getSysUser().getDeptId());
            }else{
                wmsMaterialInfo.setDeptId(wmsMaterialInfo.getDeptId());

            }
        }
        if(wmsMaterialInfo.getDeptId() != null && wmsMaterialInfo.getDeptId().longValue() !=0){
            getSession().setAttribute("treeDeptId",wmsMaterialInfo.getDeptId());
        }


        List<WmsMaterialInfo> list = wmsMaterialInfoService.selectWmsMaterialInfoList(wmsMaterialInfo);
        return getDataTable(list);
    }
    @GetMapping("/materialInfoList")
    public String materialInfoList()
    {
        return prefix + "/materialInfoList";
    }
    /**
     * 导出物料信息管理列表
     */
    @RequiresPermissions("wms:materialInfo:export")
    @Log(title = "物料信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsMaterialInfo wmsMaterialInfo)
    {
        List<WmsMaterialInfo> list = wmsMaterialInfoService.selectWmsMaterialInfoList(wmsMaterialInfo);
        ExcelUtil<WmsMaterialInfo> util = new ExcelUtil<WmsMaterialInfo>(WmsMaterialInfo.class);
        return util.exportExcel(list, "物料信息管理数据");
    }

    /**
     * 新增物料信息管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {

        mmap.put("code", getCode("M"));
        return prefix + "/add";
    }
    private String getCode(String code){
        int num =  Integer.parseInt(configService.selectConfigByKey("m.code")) ;
        SysConfig config = new SysConfig();
        config.setConfigKey("m.code");
        config.setConfigValue((++num)+"");
        configService.updateConfigValue(config);
        return StringUtils.getCode(code ,num);
    }

    /**
     * 新增保存物料信息管理
     */
    @RequiresPermissions("wms:materialInfo:add")
    @Log(title = "物料信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsMaterialInfo wmsMaterialInfo)
    {
        return toAjax(wmsMaterialInfoService.insertWmsMaterialInfo(wmsMaterialInfo));
    }

    /**
     * 修改物料信息管理
     */
    @RequiresPermissions("wms:materialInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsMaterialInfo wmsMaterialInfo = wmsMaterialInfoService.selectWmsMaterialInfoById(id);
        wmsMaterialInfo.setWmsMaterialCategory(wmsMaterialCategoryService.selectWmsMaterialCategoryByDeptId(wmsMaterialInfo.getDeptId()));
        mmap.put("wmsMaterialInfo", wmsMaterialInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料信息管理
     */
    @RequiresPermissions("wms:materialInfo:edit")
    @Log(title = "物料信息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsMaterialInfo wmsMaterialInfo)
    {
        return toAjax(wmsMaterialInfoService.updateWmsMaterialInfo(wmsMaterialInfo));
    }

    /**
     * 删除物料信息管理
     */
    @RequiresPermissions("wms:materialInfo:remove")
    @Log(title = "物料信息管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsMaterialInfoService.deleteWmsMaterialInfoByIds(ids));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {

        if(getSession().getAttribute("treeDeptId") == null){
            return AjaxResult.error("请选择左边要导入的栏目节点");
        }
        Long deptId = (Long) getSession().getAttribute("treeDeptId");
        ExcelUtil<WmsMaterialInfo> util = new ExcelUtil<WmsMaterialInfo>(WmsMaterialInfo.class);
        List<WmsMaterialInfo> userList = util.importExcel(file.getInputStream());
        String message = wmsMaterialInfoService.importMaterial(userList, deptId);
        return AjaxResult.success(message);
    }
}
